/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Modele;

import Controller.HomePageListener;
import Controller.MailPageListener;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;


/**
 * This Class is a model created in order to represents the data part of the JTable
 * receive the raw data ( ArrayList ) and add it to the Model to make it usable by the Jtable
 * @author Bakalian
 */
public class TableModel extends AbstractTableModel {
    private final ArrayList<Contact> ContactList;
 
    private static final String[] head =  {"First Name","Last Name","Home Address","Company Address","Personal Phone","Professional Phone","Home Site Address", "Email",
        "Photo addresse",
        "Age"
                };

   
 
    public TableModel(ArrayList<Contact> c) {
        super();
        this.ContactList = c;
        
    }
     /**
      * as row as the ArrayList Size
      * @return 
      */
 
    @Override
    public int getRowCount() {
        return ContactList.size();
    }
    /**
     * ad Column as the number of title of column given
     * @return 
     */
    @Override
    public int getColumnCount() {
        return head.length;
    }
  /**
   * geting it from the title array written below
   * @param columnIndex
   * @return 
   */
    @Override
    public String getColumnName(int columnIndex) {
        return head[columnIndex];
    }
  /**
   * get the value to write in the cell according to the data
   * @param rowIndex
   * @param columnIndex
   * @return 
   */
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0:
                return ContactList.get(rowIndex).getFirstName();
            case 1:
                return ContactList.get(rowIndex).getLastName();
            case 2:
                return ContactList.get(rowIndex).getHomeAddress();
            case 3:
                return ContactList.get(rowIndex).getCompanyAddress();
            case 4:
                return ContactList.get(rowIndex).getPersonalPhone();
            case 5:
                return ContactList.get(rowIndex).getProfessionalPhone();
            case 6:
                return ContactList.get(rowIndex).getHomePage();
            case 7:
                return ContactList.get(rowIndex).getEmail();
            case 8:
                return ContactList.get(rowIndex).getPhoto();
            case 9:
                return ContactList.get(rowIndex).getAge();
            default:
                return null; //Ne devrait jamais arriver
        }
    }
 
  
  /**
   * called when we click on supprimer in order to refresh table after deleting rows
   * @param rowIndex 
   */
    public void removeContact(int rowIndex) {
       // ContactList.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }
    
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true; //Toutes les cellules éditables
    }
 
 /**
  * new value to show after editing cells
  * @param aValue
  * @param rowIndex
  * @param columnIndex 
  */
    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if(aValue != null){

                
             switch(columnIndex){
                case 0:
                     ContactList.get(rowIndex).setFirstName((String)aValue);
                    break;
                case 1:
                     ContactList.get(rowIndex).setLastName((String)aValue);
                    break;
                case 2:
                     ContactList.get(rowIndex).setHomeAddress((String)aValue);
                    break;
                case 3:
                     ContactList.get(rowIndex).setCompanyAddress((String)aValue);
                    break;
                case 4:
                     ContactList.get(rowIndex).setPersonalPhone((String)aValue);
                    break;
                case 5:
                     ContactList.get(rowIndex).setProfessionalPhone((String)aValue);
                    break;
                case 6:
                     ContactList.get(rowIndex).setHomePage((String)aValue);
                     HomePageListener ex=new HomePageListener(((String)aValue));
                    break;
                case 7:
                     ContactList.get(rowIndex).setEmail((String)aValue);
                     MailPageListener ex1=new MailPageListener();
                    break;
                case 8:
                     ContactList.get(rowIndex).setPhoto((String)aValue);
                    break;
                case 9:
                     ContactList.get(rowIndex).setAge((String)aValue);
                    break;
             }
        /*     System.out.println(" FirstName: " +ContactList.get(rowIndex).getFirstName() + " LastName: " +ContactList.get(rowIndex).getLastName()
                        + "HomeAddress: " + ContactList.get(rowIndex).getHomeAddress() + "CompanyAddress: " + ContactList.get(rowIndex).getCompanyAddress() 
                        + "PersonalPhone: " + ContactList.get(rowIndex).getPersonalPhone()+" ProfessionalPhone: " + ContactList.get(rowIndex).getProfessionalPhone()
                        + "HomePage: " + ContactList.get(rowIndex).getHomeAddress()+"Email: "+ContactList.get(rowIndex).getEmail()
                        +" Age: " +ContactList.get(rowIndex).getAge() +"Photo: " +ContactList.get(rowIndex).getPhoto());
        */}
        
    }


}
